package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping
    public ResponseEntity publishComments(@RequestBody Map map) {
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentService.saveComments(movementId, comment);
        return ResponseEntity.ok(null);
    }

    @GetMapping
    public ResponseEntity commentList(String movementId,
                                      @RequestParam(defaultValue = "1") int page,
                                      @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pr = commentService.commentList(movementId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论点赞
     */
    @GetMapping("{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.commentLike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消评论点赞
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.disLikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
}
